package com.dingding.service.core.common.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.user.common.dto.common.ReqGetDriverAuthStatus;
import com.dingding.facade.user.common.dto.common.ReqRegisterDriverDTO;
import com.dingding.facade.user.common.dto.common.ReqUserLoginByWeb;
import com.dingding.facade.user.common.dto.common.ResGetDriverAuthStatus;
import com.dingding.facade.user.common.dto.common.ResUserLoginByWeb;
import com.dingding.facade.user.common.dto.special.driver.ReqSpecialDriverId;
import com.dingding.facade.user.common.dto.special.driver.ReqValidateDTO;
import com.dingding.facade.user.common.dto.special.driver.ResSpecialDriver;
import com.dingding.facade.user.facade.common.driver.IDriverFacade;
import com.dingding.facade.user.facade.common.driver.IValidateFacade;
import com.dingding.facade.user.facade.special.driver.ISpeicalDriverFacade;
import com.dingding.service.core.common.service.IUserLoginService;

/**
 * 
 * @ClassName: UserLoginServiceImpl 
 * @Description: 微信端用户登录(注册司机)
 * @author YHQ
 * @date 2016年12月5日 下午2:59:53
 * 
 */
@Component("userLoginByWebService")
public class UserLoginServiceImpl implements IUserLoginService {

	@Resource(name = "driverFacade")
	private IDriverFacade driverFacade;
	
	@Resource(name = "speicalDriverFacade")
	private ISpeicalDriverFacade specialDriverFacade;
	
	@Resource(name = "validateFacade")
	private IValidateFacade validateFacade;
	
	/**
	 * 
	 * @Title: userLoginByWeb 
	 * @Description: 微信端用户登录(司机注册)
	 * @param reqUserLoginByWeb 客户端请求Bean
	 * @author YHQ 
	 * @since V2.5.7
	 * @return ResUserLoginByWeb    返回类型
	 * 
	 */
	@Override
	public ResUserLoginByWeb userLoginByWeb(ReqUserLoginByWeb reqUserLoginByWeb) {
		
		// 验证客户端请参数是否空
		if(reqUserLoginByWeb == null || !ToolUtil.validateParam(reqUserLoginByWeb)){
			return null;
		}
		
		// 创建用户登录响应Bean(司机注册)
		ResUserLoginByWeb resUserLoginByWeb = new ResUserLoginByWeb();
		
		// 验证码是否正确
		if(validateCode(reqUserLoginByWeb)){
			
			// 获取用户Id
			this.getUserId(reqUserLoginByWeb,resUserLoginByWeb);
			// 获取用户认证状态
			this.getAuthStatus(reqUserLoginByWeb, resUserLoginByWeb);
			
		}
		
		return resUserLoginByWeb;
	}

	/**
	 * 
	 * @Title: validateCode 
	 * @Description: 验证手机验证码是否正确
	 * @param @return    设定文件 
	 * @author YHQ 
	 * @since V2.5.7
	 * @return boolean    返回类型
	 * 
	 */
	public boolean validateCode(ReqUserLoginByWeb reqUserLoginByWeb){
		
		// 创建验证手机验证码数据传输Bean
		ReqValidateDTO reqValidateDTO = new ReqValidateDTO();
		
		// 手机号
		reqValidateDTO.setTelPhone(reqUserLoginByWeb.getTelPhone());
		// 验证码
		reqValidateDTO.setValidateCode(reqUserLoginByWeb.getValidateCode());
		
		return validateFacade.validateCode(reqValidateDTO);
	}
	
	/**
	 * 
	 * @Title: getUserId 
	 * @Description: 根据手机号获取用户Id
	 * @param telPhone 用户手机号
	 * @author YHQ 
	 * @since V2.5.7
	 * @return ResUserLoginByWeb    返回类型
	 */
	public ResUserLoginByWeb getUserId(ReqUserLoginByWeb reqUserLoginByWeb,ResUserLoginByWeb resUserLoginByWeb){
		
		// 创建获取专车司机Id请求Bean
		ReqSpecialDriverId reqSpecialDriverId = new ReqSpecialDriverId();
		// 设置手机号
		reqSpecialDriverId.setTelPhone(reqUserLoginByWeb.getTelPhone());
		
		// 获取用户Id
		ResSpecialDriver resSpecialDriverId = specialDriverFacade.getSpecialDriverId(reqSpecialDriverId);
		
		// 如果获取到的用户Id为空，进行注册
		if(resSpecialDriverId == null || !ToolUtil.validateParam(resSpecialDriverId)){
			
			// 创建注册司机请求Bean
			ReqRegisterDriverDTO registerDriverDTO = new ReqRegisterDriverDTO();
			// 设置登录的用户名(手机号)
			registerDriverDTO.setUserName(reqSpecialDriverId.getTelPhone());
			
			String userId = driverFacade.registerDriver(registerDriverDTO);
			
			if(ToolUtil.isNotBlank(userId)){
				resUserLoginByWeb.setUserId(userId);
			}
			
		}else{
			resUserLoginByWeb.setUserId(resSpecialDriverId.getSuid());
		}
		
		return resUserLoginByWeb;
	}
	
	/**
	 * 
	 * @Title: getAuthStatus 
	 * @Description: 获取车主认证状态
	 * @param reqUserLoginByWeb 客户端请求Bean
	 * @param resUserLoginByWeb 响应Bean
	 * @author YHQ 
	 * @since V2.5.7
	 * @return ResUserLoginByWeb    返回类型
	 * 
	 */
	public ResUserLoginByWeb getAuthStatus(ReqUserLoginByWeb reqUserLoginByWeb,ResUserLoginByWeb resUserLoginByWeb){
		
		// 创建获取车主认证状态请求Bean
		ReqGetDriverAuthStatus reqGetDriverAuthStatus = new ReqGetDriverAuthStatus();
		
		// 设置用户Id
		reqGetDriverAuthStatus.setUserId(resUserLoginByWeb.getUserId());
		
		// 获取司机认证车主状态
		ResGetDriverAuthStatus resGetDriverAuthStatus = driverFacade.getDriverAuthStatus(reqGetDriverAuthStatus);
		
		// 判断获取司机认证状态是否为空
		if(resGetDriverAuthStatus == null || !ToolUtil.validateParam(resGetDriverAuthStatus)){
			resUserLoginByWeb.setSuvStatus(0);	// 未认证车主
		}else{
			resUserLoginByWeb.setSuvStatus(resGetDriverAuthStatus.getSuvStatus() == null?0:resGetDriverAuthStatus.getSuvStatus());
		}
		
		return resUserLoginByWeb;
	} 
	
}
